/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import net.javlov.policy.FixedOptionPolicy;

public abstract class AbstractPrimitiveAction extends AbstractOption implements Action {

	private static final long serialVersionUID = 6695367929452993940L;
	
	private Policy pi;
	
	protected AbstractPrimitiveAction() {}
	
	protected AbstractPrimitiveAction(String name) {
		this();
		setName(name);
	}
	
	@Override
	public <T> void lastStep(State<T> s, double reward) {}
	
	/**
	 * @inheritDoc
	 * 
	 * Always returns 1.
	 */
	@Override
	public <T> double getBeta(State<T> s) {
		return 1;
	}

	/**
	 * @inheritDoc
	 * 
	 * Always returns true.
	 */
	@Override
	public boolean isFinished() {
		return true;
	}

	/**
	 * @inheritDoc
	 * 
	 * Does nothing.
	 */
	@Override
	public <T> void update(State<T> s, Option o, double update) {}

	@Override
	public LearningRate getLearnRate() {
		return null;
	}

	@Override
	public void setLearnRate(LearningRate rate) {
		throw new UnsupportedOperationException("Can't set the learning rate of a primitive action.");
	}

	@Override
	public Policy getPolicy() {
		if ( pi == null ) {
			try {
				pi = new FixedOptionPolicy(getClass().newInstance());
			} catch (InstantiationException e) {
				pi = new FixedOptionPolicy(this);
			} catch (IllegalAccessException e) {
				pi = new FixedOptionPolicy(this);
			}
		}
		return pi;
	}

	@Override
	public void setPolicy(Policy p) {
		throw new UnsupportedOperationException("Can't set the policy of a primitive action.");
	}

}
